package com.zhifei.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhifei.common.utils.Constant;
import com.zhifei.common.utils.DispatcherUtils;
import com.zhifei.common.utils.Result;
import com.zhifei.system.domain.DataSourceEntity;
import com.zhifei.system.domain.dto.DataSourceDTO;
import com.zhifei.system.domain.dto.DeleteDTO;
import com.zhifei.system.domain.vo.DataSourceVo;
import com.zhifei.system.mapper.DataSourceMapper;
import com.zhifei.system.service.DataSourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSourceEntity> implements DataSourceService {


    @Override
    public Result add(DataSourceDTO dto) {
        DataSourceEntity entity = new DataSourceEntity();
        BeanUtils.copyProperties(dto,entity);
        baseMapper.insert(entity);
        return Result.success(entity);
    }

    @Override
    public Result update(DataSourceDTO dto) {
        DataSourceEntity entity = new DataSourceEntity();
        BeanUtils.copyProperties(dto,entity);
        entity.setId(DispatcherUtils.parseLong(dto.getId()));
        baseMapper.updateById(entity);
        return Result.success(entity);
    }

    @Override
    public void delete(DeleteDTO dto) {
        DataSourceEntity entity = new DataSourceEntity();
        entity.setId(DispatcherUtils.parseLong(dto.getId()));
        entity.setDeleteStatus(1);
        baseMapper.updateById(entity);
    }

    @Override
    public List<DataSourceVo> queryList() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("delete_status",0);
        wrapper.orderByDesc("create_time");
        List<DataSourceEntity> entityList = baseMapper.selectList(wrapper);
        return entityToVoList(entityList);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        Page page = new Page(Long.parseLong((String)params.get(Constant.PAGE)),Long.parseLong((String)params.get(Constant.LIMIT)));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("delete_status",0);
        wrapper.orderByDesc("create_time");
        IPage list = baseMapper.selectPage(page,wrapper);
        List<DataSourceVo> voList = entityToVoList(list.getRecords());
        page.setRecords(voList);
        return page;
    }

    public List<DataSourceVo> entityToVoList(List<DataSourceEntity> entityList) {
        List<DataSourceVo> voList = new ArrayList<>();
        if (entityList != null && entityList.size() > 0) {
            for (DataSourceEntity entity : entityList) {
                DataSourceVo vo = entityToVo(entity);
                voList.add(vo);
            }
        }
        return voList;
    }

    public DataSourceVo entityToVo(DataSourceEntity entity) {
        DataSourceVo vo = new DataSourceVo();
        BeanUtils.copyProperties(entity,vo);
        vo.setId(DispatcherUtils.parseString(entity.getId()));
        vo.setCreateTime(DispatcherUtils.parseDateTimeStr(entity.getCreateTime()));
        return vo;
    }

}
